﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ComponentFactory.Krypton.Toolkit;
using System.ComponentModel;
using System.Windows.Forms;

namespace EnzymeLaborTool
{
    public class MyKryptonGridView : KryptonDataGridView
    {

        // Fields
        private IContainer components;
        private bool disableNavigation;
        private KeyEventArgs lastKeyEventArgs;
        private MouseEventArgs lastMouseEventArgs;
        private string lastNavRequestSource;

        // Events
        public event EventHandler NavigationRejected;

        // Methods
        public MyKryptonGridView()
        {
            this.InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.components = new Container();
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (!this.disableNavigation)
            {
                base.OnKeyDown(e);
            }
            else
            {
                this.lastKeyEventArgs = e;
                this.lastNavRequestSource = "Keyboard";
                if (this.NavigationRejected != null)
                {
                    this.NavigationRejected(this, null);
                }
            }
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            DataGridView.HitTestInfo info = base.HitTest(e.X, e.Y);
            if (!this.disableNavigation || (info.RowIndex == base.CurrentCell.RowIndex))
            {
                base.OnMouseDown(e);
            }
            else
            {
                this.lastMouseEventArgs = e;
                this.lastNavRequestSource = "Mouse";
                if (this.NavigationRejected != null)
                {
                    this.NavigationRejected(this, null);
                }
            }
        }

        public void ResumeNavigation()
        {
            if (this.lastNavRequestSource == "Mouse")
            {
                DataGridView.HitTestInfo info = base.HitTest(this.lastMouseEventArgs.X, this.lastMouseEventArgs.Y);
                this.SetCurrentCellAddressCore(info.ColumnIndex, info.RowIndex, false, false, true);
                foreach (DataGridViewCell cell in base.SelectedCells)
                {
                    this.SetSelectedCellCore(cell.ColumnIndex, cell.RowIndex, false);
                }
                this.SetSelectedCellCore(info.ColumnIndex, info.RowIndex, true);
            }
            else if (this.lastNavRequestSource == "Keyboard")
            {
                foreach (DataGridViewCell cell2 in base.SelectedCells)
                {
                    this.SetSelectedCellCore(cell2.ColumnIndex, cell2.RowIndex, false);
                }
                if (this.lastKeyEventArgs.KeyCode == Keys.Up)
                {
                    int rowIndex = Math.Max(base.CurrentCell.RowIndex - 1, 0);
                    int columnIndex = base.CurrentCell.ColumnIndex;
                    this.SetCurrentCellAddressCore(columnIndex, rowIndex, false, false, false);
                    this.SetSelectedCellCore(columnIndex, rowIndex, true);
                }
                if (this.lastKeyEventArgs.KeyCode == Keys.Down)
                {
                    int num3 = Math.Min((int)(base.CurrentCell.RowIndex + 1), (int)(base.RowCount - 1));
                    int num4 = base.CurrentCell.ColumnIndex;
                    this.SetCurrentCellAddressCore(num4, num3, false, false, false);
                    this.SetSelectedCellCore(num4, num3, true);
                }
            }
            this.lastNavRequestSource = "";
            this.lastKeyEventArgs = null;
            this.lastMouseEventArgs = null;
        }

        // Properties
        public bool DisableNavigation
        {
            get
            {
                return this.disableNavigation;
            }
            set
            {
                this.disableNavigation = value;
                base.ReadOnly = value;
                bool disableNavigation = this.disableNavigation;
            }
        }
    }


}
